let db = require('../db.js');

//设备数据拉取

exports.noticeList = async  function(start,length) {
 
    start = parseInt(start);
    length = parseInt(length);
     let sql=`
     SELECT SQL_CALC_FOUND_ROWS *,
     DATE_FORMAT(createdAt,'%Y-%m-%d %H:%i') AS createdAt
     FROM   notice 
     `
     sql+=` limit :start,:length ;`
     sql += `   SELECT FOUND_ROWS() sum;`
     sql = sql.replace('AND', 'WHERE');
 
     let data = await db.query(sql,{start,length});
     return data[0];
 }

 //添加通知公告

exports.addNotice  = async function(title,noticeType,content,img){
    let sql=`
    INSERT  INTO notice(title,content,img,noticeType)
    VALUES(:title,:content,:img,:noticeType)
    `
    let  data  =await db.query(sql,{title,content,img,noticeType});
    return data[0];
    }
    
    //获取单个通知公告的详情

exports.noticeDetail  = async function(noticeId){
    let sql=`
    SELECT  *,
    DATE_FORMAT(createdAt,'%Y-%m-%d %H:%i') AS createdAt
    FROM  notice WHERE id =:noticeId 
    
    `
    let data  = await db.query(sql,{noticeId});
    return data[0];
    }

    
//修改通知公告内容

exports.updateNotice = async function(id,title,noticeType,content,img){
    let sql=`
    UPDATE  notice SET 
    title=:title,
    noticeType =:noticeType,
    content =:content,
    img=:img
    where id=:id
    
    `
    let data  =await db.query(sql,{id,title,noticeType,content,img});
    return data[0];
    }
    //删除通知公告

exports.removeNoitce = async function(noticeId){
    let sql=`
   delete from notice
    where id=:noticeId
    `
 let data =await db.query(sql,{noticeId});
 return data[0];   
}
